package com.lizhenhua.fmmall.controller;

import com.lizhenhua.fmmall.entity.Users;

import com.lizhenhua.fmmall.mapper.UsersMapper;
import com.lizhenhua.fmmall.service.serviceImpl.UserServiceImpl;
import com.lizhenhua.fmmall.utils.Base64Utils;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

//@Controller
//@ResponseBody //所有的都为异步请求  返回的都是JSON
@RestController //@RestController = @Controller+@ResponseBody

@RequestMapping("/user")
// tags 是在页面中显示的标题
@Api(value = "用户管理", tags = "提供用户登录和注册接口")
@CrossOrigin // 解决跨域问题 这样的数据发送到前端是不会被阻挡的
public class UserController {
    @Autowired
    UserServiceImpl userService;
//    @Resource
//    private Logger logger = LoggerFactory.getLogger(UserController.class);



    @RequestMapping("/getUsers")
    public ResultVo getUsers(Integer limit){

        return userService.usersList(limit);
    }



    //方法注解 说明接口的作用
    @ApiOperation("用户登录接口")
    // 方法注解说明方法参数的作用
    @ApiImplicitParams({
//       dataType数据类型     required = true 必须填写  defaultValue默认值
            @ApiImplicitParam(dataType = "string", name = "userName", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "pwd", value = "用户登录密码", required = true)
    })
    @GetMapping("/login")
    public ResultVo login(String username, String pwd) {
        ResultVo resultVo = userService.checkLogin(username, pwd);
        // 如果验证登陆成功了，则需要生成令牌token() token就是按照特定规则生成的字符串
//        String token = Base64Utils.encode(userName+ 123456);
//        resultVo.setMsg(token);

//        logger.info(resultVo.getMsg());
        return resultVo;
    }

    //@ApiIgnore 添加此注解的接口不会生成到文档中
    @ApiOperation("用户注册接口")
    @ApiImplicitParams({
//       dataType数据类型     required = true 必须填写  defaultValue默认值
            @ApiImplicitParam(dataType = "string", name = "username", value = "用户注册账号", required = true),
            @ApiImplicitParam(dataType = "string", name = "password", value = "用户注册密码", required = true)
    })
//    @ApiImplicitParam(value = "用户注册信息", dataType = "Users", required = true)
    @PostMapping("/regist")
    // 如果参数是一个对象 那么我们应该在实体类中添加注解
    public ResultVo regist(@RequestBody Users users) {
        ResultVo resultVo = userService.userResgit(users.getUsername(), users.getPassword());
        return resultVo;
    }


    @GetMapping("/delete/{uid}")
    public ResultVo deleteUser(@PathVariable("uid") int uid) {


        System.out.println("----------------deleteUser");
        return new ResultVo(100, "delete success", null);
    }


    @ApiOperation("校验token是否过期接口")
    @GetMapping ("/userTokenCheck")
    public ResultVo userTokenCheck(@RequestHeader("token") String token) {


       return new ResultVo(ResStatus.OK,"success",null);
    }


}
